package 数组;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author TTDB
 * @create 2025/9/14
 */
public class merge {
    public int[][] merge(int[][] intervals){
        int n = intervals.length;
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        List<int[]> res = new ArrayList<>();
        for(int i = 0; i < n; i++){
            int L = intervals[i][0], R = intervals[i][1];
            while(i < n - 1 && R >= intervals[i + 1][0]){
                R = Math.max(R, intervals[i + 1][1]);
                i++;
            }
            res.add(new int[]{L, R});
        }
        return res.toArray(new int[res.size()][]);
    }
}
